<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8"/>
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
		<meta name="HandheldFriendly" content="true"/>
		<meta name="MobileOptimized" content="320"/>
		<title>Hello H5+</title>
		<script type="text/javascript" src="../js/common.js"></script>
		  <script src="../js/mui.min.js"></script>
    	<script src="../js/binaryajax.js" type="text/javascript" charset="utf-8"></script>
    	<script src="../js/exif.js" type="text/javascript" charset="utf-8"></script>
    	<script src="../js/canvasResize.js" type="text/javascript" charset="utf-8"></script>
		<script src="../js/wf.js"></script>
		 <script src="../js/jquery-1.7.2.min.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
		var files=[];
		var index=1;
		var f1 = new Array();
		(function(doc) {
			mui.init();
			mui.plusReady(function() {	
			var self = plus.webview.currentWebview();
			var username = self.username;	
			var value=self.value;
			var proid=self.proid;
			var Datas=new Object();
			Datas.ProID=proid;
			Datas.CreatePeople=username;
			Datas.value=value;
			$("#btnUpload").on("tap",function(){	
				Datas.Safe=$("#Safe").val();
				Datas.Quality=$("#Quality").val();
				Datas.Measure=$("#Measure").val();
				Datas.FileArr=f1.toString();
				Upload(Datas);
				});				
			
			});
		}(document));
		
		// 拍照添加文件
		function appendByCamera(){	
			var cmr = plus.camera.getCamera();
	        cmr.captureImage(function(p) {
		            plus.io.resolveLocalFileSystemURL(p, function(entry) {
		            var localurl = entry.toLocalURL(); //把拍照的目录路径，变成本地url路径，例如file:///........之类的。
		            appendFile(localurl);
	            	});
		        }, function(e) {
		        mui.toast("很抱歉，获取失败 "+e);
	    	});
		}
		// 从相册添加文件
		function appendByGallery(){
			plus.gallery.pick(function(p){
		        appendFile(p);
		    });
		}
	
	 
		function showImg(url) {
			if(f1.length>3){
				mui.toast("仅支持上传4张图片！");
				return null;
			}
			plus.nativeUI.showWaiting();
		    // 兼容以“file:”开头的情况
		    if (0 != url.toString().indexOf("file:///")) {   	
		        url = "file://" + url;
		      }
		    var _div_ = document.getElementsByClassName("showimg")[0];
		    var _img_ = new Image();
		    _img_.src = url; // 传过来的图片路径在这里用。
		        _img_.onclick = function() {
		                plus.runtime.openFile(url);
		            };
		    _img_.onload = function() {
		        var tmph = _img_.height;
		        var tmpw = _img_.width;
		        var isHengTu = tmpw > tmph;
		        var max = Math.max(tmpw, tmph);
		        var min = Math.min(tmpw, tmph);
		        var bili = min / max;
		        if (max > 1200) {
		            max = 1200;
		            min = Math.floor(bili * max);
		        }
		        tmph = isHengTu ? min : max;
		        tmpw = isHengTu ? max : min;
		        _img_.style.border = "1px solid rgb(200,199,204)";
		        _img_.style.margin = "10px";
		        _img_.style.width = "90%";
//		        _img_.style.height = "80px";
		        _img_.onload = null;
		        plus.io.resolveLocalFileSystemURL(url, function(entry) {
		                entry.file(function(file) {
		                    console.log(file.size + '--' + file.name);
		                    canvasResize(file, {
		                        width: tmpw,
		                        height: tmph,
		                        crop: false,
		                        quality: 30, //压缩质量
		                        rotate: 0,
		                        callback: function(data, width, height) {
		                            f1.push(data);
		                            console.log(data.toString());
		                            _img_.src = data;
		                            _div_.appendChild(_img_);
		                            plus.nativeUI.closeWaiting();
		                        }
		                    });
		                });
		            },
		            function(e) {
		                plus.nativeUI.closeWaiting();
		                console.log(e.message);
		            });
		    };
		};
		
		function uploadpics(url){
			var img=new Image();
			img.src=url;
			img.onload=function(){
				
				var that=img;
				var w=that.width,
				h=that.height,
				scale=w/h;
				w=320||w;
				h=w/scale;
				var canvas=document.createElement('canvas');
				var ctx=canvas.getContext('2d');
				$(canvas).attr({
					width:w,
					height:h
				})
				ctx.drawImage(that,0,0,w,h);
				var base64=canvas.toDataURL('image/jpeg',1||0.8);
				f1.push(base64);
			}
			
		}
					
		// 添加文件
	
		function appendFile(p){			
			empty.style.display="none";
			showImg(p);	
		}
		// 产生一个随机数
		function getUid(){
			return Math.floor(Math.random()*100000000+10000000).toString();
		}
		function Upload(Datas) {
			if(f1.length<=0){
				plus.nativeUI.alert("没有添加上传图片！");
				return;
			}
		    plus.nativeUI.showWaiting();
		    wf.ajax("SafeQuality/SafeQualityAdd",{
			traditional :true, 
			type: "post",	
			data:Datas,
			dataType:"json",
			success:function(resule){	
			if (resule.Code == "20000") {								
				plus.nativeUI.closeWaiting();
				mui.toast(resule.Msg);
				//跳转到首页
				refreshParent();
				}else{
					plus.nativeUI.closeWaiting();
					mui.toast(resule.Msg);
					}
				}
			});      	
		}
		function refreshParent(){
		var main = plus.webview.getWebviewById('SafeQualitylistsub');//或者通过A页面id获取A页面对象
		main.reload(true);
			mui.fire(main, "pageflowrefresh" );
			mui.back();
		}

		</script>
		<link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
	<link rel="stylesheet" href="../css/mui.min.css">
	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" onclick="back(true);"></a>

			<h1 class="mui-title">安全质量添加</h1>
			
		</header>	
		<div id="dcontent" class="dcontent">
			<br />
			<p class="heading">安全管理：</p>
			<div class="mui-input-row" style="margin: 15px 15px;">
				<input type="text" id="Safe">
			</div>
			<p class="heading">质量管理：</p>
			<div class="mui-input-row" style="margin: 15px 15px;">
				<input type="text" id="Quality">
			</div>
			<p class="heading">措施：</p>
			<div class="mui-input-row" style="margin: 15px 15px;">
				<input type="text"  id="Measure">
			</div>

			<p class="heading">上传图片列表：</p>
			<ul id="files" style="text-align:left;">
				<p id="empty" style="font-size:12px;color:#C6C6C6;">无上传图片</p>
			</ul>
			  <div class="showimg" >

            </div>
			<table style="width:100%;">
				<tbody>
					<tr>
						<td style="width:40%"><div class="button button-select" onclick="appendByCamera()">拍照</div></td>
						<td style="width:40%"><div class="button button-select" onclick="appendByGallery()">相册选取</div></td>
					</tr>
				</tbody>
			</table>
			<br/>
			<div class="button" id="btnUpload">上 传</div>
			<br/>
			<!--<ul class="dlist">
				<li class="ditem" onclick="appendByCamera()">拍照添加文件</li>
				<li class="ditem" onclick="appendByGallery()">相册添加文件</li>
			</ul>-->
		</div>
	</body>
	<script type="text/javascript" src="../js/immersed.js" ></script>
</html>